#include "widget.h"
#include "ui_widget.h"
#include <QLabel>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    //初始化标签页
    QLabel *label1 = new QLabel(ui->tab);
    label1->setText("这是标签页1");
    label1->resize(200, 100);

    QLabel *label2 = new QLabel(ui->tab_2);
    label2->setText("这是标签页2");
    label2->resize(200, 100);
}

Widget::~Widget()
{
    delete ui;
}

//添加标签页
void Widget::on_pushButton_clicked()
{
    //获取标签页数量
    int count = ui->tabWidget->count();
    //创建标签页对象
    QWidget* tab = new QWidget();
    ui->tabWidget->addTab(tab, QString("Tab ") + QString::number(count + 1));
    //并选中这个新的标签页
    ui->tabWidget->setCurrentIndex(count);
}

//删除标签页
void Widget::on_pushButton_2_clicked()
{
    //获取选中的下标
    int index = ui->tabWidget->currentIndex();
    //删除
    ui->tabWidget->removeTab(index);
}
